# coding: utf-8

import os
import time
from datetime import datetime
from selenium.common import NoSuchElementException, \
    ElementNotInteractableException

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.common.exceptions import NoSuchWindowException
from selenium.webdriver.chrome.service import Service
from .tools import send_log, convert_cookies, write_cms_to_excel, \
    merge_xlsx_files, send_email_with_message, handle_asset

from settings import USR_DATA_DIR, RESULT_PATH
from ..tools import get_webdriver, connect_running_driver


def run_crawler(tag_list, create_time, update_time, craw_number,
                email_address_list, error_log, loop_crawl,
                upload_file_name, out_log_file_name):

    current_ip = ""
    is_completed = None
    print("----------------------爬虫程序启动----------------------")
    send_log(out_log_file_name, "爬虫程序准备完成")
    # 获取 driver 游标
    driver = get_webdriver(USR_DATA_DIR)
    send_log(out_log_file_name, "请在30s内选择用户")
    time.sleep(30)
    retry_number = 3
    while retry_number:
        retry_number -= 1
        try:
            driver = connect_running_driver()
            print('开始采集...')
            driver.get("http://www.google.com")
            time.sleep(5)
            print(driver.title)
            cookies = driver.get_cookies()
            print(cookies)
            # 页面最大化
            driver.maximize_window()

            URL = 'https://studio.youtube.com/owner/assets?filter=[{"isPinned":true,"name":"ASSET_OWNERSHIP","value":"NOT_MY_ASSETS"}]&sort={"columnType":"assetCreated","sortOrder":"DESCENDING"}'
            if create_time is not None:
                URL = 'https://studio.youtube.com/owner/assets?filter=[{"isPinned":true,"name":"ASSET_OWNERSHIP","value":"NOT_MY_ASSETS"}]&sort={"columnType":"assetCreated","sortOrder":"DESCENDING"}'
            if update_time is not None:
                URL = 'https://studio.youtube.com/owner/assets?filter=[{"isPinned":true,"name":"ASSET_OWNERSHIP","value":"NOT_MY_ASSETS"}]&sort={"columnType":"assetUpdated","sortOrder":"DESCENDING"}'

            driver.get(URL)
            cookies = driver.get_cookies()
            print(cookies)

            # formatted_cookies = convert_cookies(cookies)

            # 添加之前获取的Cookies
            for cookie in cookies:
                driver.add_cookie(cookie)

            now = datetime.now()
            date_time = now.strftime("%Y%m%d%H%M%S")
            if upload_file_name:
                # 获取已存在的文件
                file_name = os.path.basename(upload_file_name)
                craw_result = RESULT_PATH + file_name.rsplit(".")[
                    0] + date_time
            else:
                craw_result = RESULT_PATH + date_time
            if not os.path.exists(craw_result):
                    os.makedirs(craw_result)

            exsit_file_list = [f for f in os.listdir(craw_result) if
                               "xlsx" in f]
            print(f"exsit file list is : {exsit_file_list}")

            current_num = 0
            tag_list_crawl = []
            for tag in tag_list:
                if tag is None or tag == "":
                    continue
                if tag + ".xlsx" in exsit_file_list:
                    current_num += 1
                    continue
                tag_list_crawl.append(tag)

            print(f"tag crawl list is : {tag_list_crawl}")
            total_num = len(tag_list)
            l_num = total_num - current_num

            print(
                f"爬取总数:{total_num},已发现爬取个数:{current_num}，还剩未爬取个数{l_num}"
            )
            send_log(out_log_file_name,
                     f"爬取总数:{total_num},已发现爬取个数:{current_num}，还剩未爬取个数{l_num}")

            print(f"开始爬取")
            send_log(out_log_file_name, "开始爬取")

            for tag in tag_list_crawl:
                current_ip = tag
                SEND_MAIL = False
                current_num += 1
                l_num = total_num - current_num
                print(f"总数:{total_num},还剩:{l_num}")
                send_log(out_log_file_name, f"总数:{total_num}, 还剩:{l_num}")

                filter_parm = [{"isPinned": "true", "name": "ASSET_OWNERSHIP",
                                "value": "NOT_MY_ASSETS"},
                               {"name": "QUERY",
                                "value": {"name": "IS", "value": tag}}]
                sort_parm = {"columnType": "assetCreated",
                             "sortOrder": "DESCENDING"}
                URL = f"https://studio.youtube.com/owner/assets?filter={str(filter_parm)}&{str(sort_parm)}"
                print(URL)

                no_result = False
                while True:
                    try:
                        input_element = driver.find_element(By.XPATH,
                                                            '//*[@id="text-input"]')
                        time.sleep(1)
                        input_element.send_keys(tag)
                        time.sleep(1)
                        input_element.send_keys(Keys.ENTER)

                        wait = Wait(driver, 120, 0.5)
                        wait.until(EC.any_of(
                            EC.presence_of_element_located(
                                (By.CLASS_NAME, "no-content")),
                            EC.element_to_be_clickable(
                                (By.XPATH, '//*[@id="asset-link"]'))
                        ))

                        links = driver.find_elements(By.XPATH,
                                                     '//*[@id="asset-link"]')
                        if len(links) < 1:
                            print("没有查询到资产")
                            send_log(out_log_file_name, "没有查询到资产")
                            cms_list = []
                            write_cms_to_excel(
                                craw_result + "/" + tag + ".xlsx", cms_list)
                            time.sleep(2)
                            driver.find_element(By.XPATH,
                                                '//*[@id="delete-icon"]').click()
                            time.sleep(2)
                            no_result = True
                            break

                        links[0].click()
                        time.sleep(5)
                        break

                    except (
                    NoSuchElementException, ElementNotInteractableException):
                        print("ElementNotInteractableException")
                        send_log(out_log_file_name,
                                 "ElementNotInteractableException")
                        driver.get(URL)
                        continue

                if no_result:
                    continue

                # 资产处理逻辑
                craw_result = handle_asset(
                    driver, tag, email_address_list, craw_number,
                    create_time, craw_result,
                    update_time, out_log_file_name)

            print("######################爬取结束############################")
            send_log(out_log_file_name,
                     "######################爬取结束############################")
            merge_xlsx_files(craw_result,
                             craw_result + "/final_result.xlsx")
            send_log(out_log_file_name, "合并爬取结果结束")
            is_completed = True
            send_log(out_log_file_name, "Finished")
            break
        except Exception as e:
            is_aborted = True
            print(e)
            send_log(out_log_file_name, str(e))
            print("程序发生错误请重新爬取")
            send_log(out_log_file_name, "程序发生错误请重新爬取")
            driver.refresh()
            time.sleep(5)
            write_cms_to_excel("error_log.xlsx", [current_ip])

            #if error_log:
            #    send_email_with_message(email_address_list,
            #                            f"爬虫出错,出错ID为:{current_ip}，出错原因:{str(e)}",
            #                            out_log_file_name)
            if current_ip in tag_list:
                tag_list.remove(current_ip)

        finally:
            is_running = False
    if not is_completed:
        driver.quit()
